package cn.edu.scnu.controller;

import cn.edu.scnu.entity.Movie;
import cn.edu.scnu.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping
    public String searchPage() {
        return "search";  // 返回位于 src/main/resources/templates/search.html 的模板文件
    }

    @PostMapping("/movieList")
    @ResponseBody
    public Map<String, Object> searchMovies(@RequestParam String query,
                                            @RequestParam int page,
                                            @RequestParam int size) {
        List<Movie> movies = searchService.searchMovies(query, page, size);
        int total = searchService.countSearchMovies(query);
        Map<String, Object> response = new HashMap<>();
        response.put("movies", movies);
        response.put("total", total);
        return response;
    }
}
